//
//  CategoryChoiceViewController.m
//  DemoCoreData
//
//  Created by tangkexiong on 16/3/16.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "CategoryChoiceViewController.h"

#import "YCCoreDataManager+CDProductCategory.h"

#import "CategoryEditViewController.h"

@interface CategoryChoiceViewController ()<CategoryEditVCDelegate> {
    NSArray *_categoryArray;
}

@end

@implementation CategoryChoiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(loadInfo) forControlEvents:UIControlEventValueChanged];
    
    [self.refreshControl beginRefreshing];
    [self loadInfo];
}

#pragma mark - Control

- (void)loadInfo {
    [[YCCoreDataManager sharedInstance] selectCategoriesBlock:^(NSError *error, NSArray<YCProductCategory *> *array) {
        if (error == nil) {
            _categoryArray = array;
            
            [self.tableView reloadData];
        } else {
            
        }
        
        [self.refreshControl endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _categoryArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    YCProductCategory *category = _categoryArray[indexPath.row];
    
    cell.textLabel.text = category.mName;
    
    if (_category.mId && [category.mId isEqualToNumber:_category.mId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YCProductCategory *category = _categoryArray[indexPath.row];
    
    if ([_myDelegate respondsToSelector:@selector(categoryChoiceVC:didSelectCategory:)]) {
        [_myDelegate categoryChoiceVC:self didSelectCategory:category];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.refreshControl beginRefreshing];
        
        YCProductCategory *category = _categoryArray[indexPath.row];
        
        [[YCCoreDataManager sharedInstance] deleteCategory:category block:^(NSError *error) {
            if (error == nil) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:_categoryArray];
                [array removeObject:category];
                _categoryArray = array;
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            
            [self.refreshControl endRefreshing];
        }];
    }
}

#pragma mark - CategoryEditVCDelegate

- (void)categoryEditVC:(CategoryEditViewController *)vc didEdit:(YCProductCategory *)category isAdd:(BOOL)isAdd {
    NSMutableArray *array = [NSMutableArray arrayWithArray:_categoryArray];
    
    if (isAdd) {
        [array addObject:category];
        
        _categoryArray = array;
        
        [self.tableView reloadData];
    } else {
        NSInteger index = [_categoryArray indexOfObject:category];
        
        [array replaceObjectAtIndex:index withObject:category];
        
        _categoryArray = array;
        
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
    }
    
    [self.navigationController popToViewController:self animated:YES];
}

- (IBAction)cancelBtnClicked:(id)sender {
    if ([_myDelegate respondsToSelector:@selector(categoryChoiceVC:didSelectCategory:)]) {
        [_myDelegate categoryChoiceVC:self didSelectCategory:nil];
    }
}

- (IBAction)addBtnClicked:(id)sender {
    CategoryEditViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"vcCategoryEdit"];
    vc.myDelegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
